﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Globalization;

namespace VsSpeakerCompanion
{
    /// <summary>
    /// Very simple logger used for dumping unhandled exceptions to disk
    /// </summary>
    internal class Logger
    {
        // Full path to log file
        private static string LogFile;

        static Logger()
        {
            // The path to the file will be the path from where Demo Dashboard was loaded
            // This is the file you may want to look at for hints when dealing with potential bugs, etc.
            LogFile = Path.Combine(Path.GetDirectoryName(typeof(Logger).Assembly.Location), @"log.txt");
        }

        /// <summary>
        /// Logs a message
        /// </summary>
        /// <param name="message">text message to log</param>
        /// <param name="args">variable list of arguments to log</param>
        internal static void Log(string message, params string[] args)
        {
            try
            {
                // Make sure we have a file to write to
                EnsureLogFile();
                // TODO: Although the logged info should be minimal not reciclying the log file could be an issue sooner than later
                File.AppendAllText(LogFile, GetMessage(message, args));
            }
            catch { }
        }


        /// <summary>
        /// Used to format a message with some common formatting before logging it
        /// </summary>
        /// <param name="message"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        private static string GetMessage(string message, params object[] args)
        {
            string result = message;

            if (args != null)
            {
                result = string.Format(CultureInfo.CurrentUICulture, message, args);
            }

            return DateTime.Now.ToString() + ": " + result + Environment.NewLine;
        }

        /// <summary>
        /// Logs an exception and its details to disk
        /// </summary>
        /// <param name="ex"></param>
        internal static void Log(Exception ex)
        {
            Log(ex.GetType().FullName + ": " + ex.Message);
            if (ex.InnerException != null)
            {
                Log(ex.InnerException);
            }
        }

        /// <summary>
        /// Make sure we always have a log file to write to
        /// </summary>
        private static void EnsureLogFile()
        {
            if (File.Exists(LogFile))
            {
                if ((File.GetAttributes(LogFile) & FileAttributes.ReadOnly) != 0)
                {
                    File.SetAttributes(LogFile, FileAttributes.Normal);
                }
            }
            else
            {
                string path = Path.GetDirectoryName(LogFile);

                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }
            }
        }
    }
}
